#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;
typedef struct LNode{
    ElemType data;
    struct LNode *next;
}LNode,*LinkList;
void tail_insert(LinkList &L)
{
    L=(LinkList)malloc(sizeof(LNode));
    L->next=NULL;
    ElemType x;
    scanf("%d",&x);
    LinkList s,r=L;
    while (x!=9999)
    {
        s=(LinkList)malloc(sizeof(LNode));
        s->data=x;
        r->next=s;
        r=s;
        scanf("%d",&x);
    }
    r->next=NULL;  //r->next=NULL;
}

LinkList GetElem(LinkList L,int SearchPos)
{
    int i=0;
    if(SearchPos<0)
    {
        return NULL;
    }
    while (L&&i<SearchPos)
    {
        L=L->next;
        i++;
    }
    return L;
}
void ListFrontInsert(LinkList L,int pos,ElemType value)
{
    LinkList p= GetElem(L,pos-1);
    LinkList q;
    q=(LinkList)malloc(sizeof(LNode));
    q->data=value;
    q->next=p->next;
    p->next=q;
}
void ListDelete(LinkList L,int pos)
{
    LinkList p= GetElem(L,pos-1);
    LinkList q=p->next;
    p->next=q->next; //断链
    free(q);
}
void printf_list(LinkList L)
{
    L=L->next;
    while (L!=NULL)
    {
        printf("%3d",L->data);
        L=L->next;
    }
    printf("\n");
}
int main() {
    LinkList L;
    LinkList search;

    tail_insert(L);

    search=GetElem(L,2);
    printf("%d\n",search->data);

    ListFrontInsert(L,2,99); //3 4 5 6 7 9999
    printf_list(L);

    ListDelete(L,4);
    printf_list(L);
    return 0;
}
